import torch

from config import device


class UniformCrossover:
    @staticmethod
    def do(parent0, parent1, problem=None, replace_prob=0.3):
        var_dim = problem.var_dim
        mu = torch.rand((1, var_dim), device=device)
        child0 = torch.zeros((1, var_dim), dtype=float, device=device)
        child1 = torch.zeros((1, var_dim), dtype=float, device=device)
        child0[mu <= replace_prob] = parent0[mu <= replace_prob]
        child0[mu > replace_prob] = parent1[mu > replace_prob]
        child1[mu <= replace_prob] = parent1[mu <= replace_prob]
        child1[mu > replace_prob] = parent0[mu > replace_prob]
        return torch.cat((child0, child1), dim=0)
